#include"stdafx.h"
#include"read_fertrotation.h"
Par_Maps read_fertrotation::par_fert_props;
int read_fertrotation::Flag_par_fert_props=0;

 int read_fertrotation::read_fertrot (IN_read_fertrotation &input)
	{
		// builds up a fertrotation from entries in fertrot file
		// 1. There are two cases 
			//1.1 For a site  A L L dates are COMPLETE and fert amounts are given
			//1.2 For a site O N L Y the YEARS of fertilizer application are given	(months days are negative)
				 // In this case there is another split
					// 1.2.1 More applications per year. In this case dates will be searched automatically
					// 1.2.2.Just one application per year. In this case the amount will be split by a decision system in dependence 
							 // of the respective crop
		//units: input in T O N S freshmatter [t FM]

		//1. Test if parameter sets are loaded
		if (Flag_par_fert_props==0){
			Flag_par_fert_props=1;
			read_Parameter_Maps();
		}

		int firstentry_pos,lastentry_pos;
		int loopcounter=0;
		int row;
		int dec,dec1;
		int year,mon;
		double day;
		string idstring;
		
		input.fertrot->ptr->set_clear();

		//search for first/last value
		firstentry_pos=input.fertlist->get_pos_of_first_entry(input.name);
		lastentry_pos=input.fertlist->get_pos_of_last_entry(input.name);
		if (firstentry_pos<0)
			return 1;
		// First decision: Complete dates or not
		dec=0;dec1=0;
		for(row=firstentry_pos;row<=lastentry_pos;row++)
		{
			year=(int)input.fertlist->var["year"][firstentry_pos];
			mon=(int)input.fertlist->var["mon"][firstentry_pos];
			day=input.fertlist->var["day"][firstentry_pos];
			dec=dec+((year>=0)&&(mon>=0)&&(day>=0));
			dec1=dec1+(year>0);

		}
		// Case 1.1 Complete Data
		if(dec==(lastentry_pos-firstentry_pos+1)) 
			read_sitespecific_fert_history_with_complete_date(input,firstentry_pos,lastentry_pos);
		//Case 1.2.1 Incomplete Data
		else if(dec1==(lastentry_pos-firstentry_pos+1))
			read_sitespecific_fert_history_with_year_date(input,firstentry_pos,lastentry_pos);
		else err::err2("Rotation_Reader_Fert::read_fertrot_from_fertrot: date configuration is quite strange");

		return 0;
	}
 int read_fertrotation::read_sitespecific_fert_history_with_complete_date(
		IN_read_fertrotation &input,int firstentry_pos,int lastentry_pos)
	{
	

		int count;
		timestamp stamp1,stamp2;
		time_intervall ti;
		string root_err_mess="Rotation_Reader_Fert::read_sitespecific_fert_history_with_complete_date:";
		count=firstentry_pos;
		vector<double> nullvect;
		string idstring;
		map< string,vector<double> >::iterator it,itcount;
		do{
			 stamp1.set((int)input.fertlist->var["year"][count],(int)input.fertlist->var["mon"][count],input.fertlist->var["day"][count]);
			 stamp2=stamp1;
			 stamp2.set_add_days(1);
			 ti.set(stamp1,stamp2);
			 input.fertrot->ptr->point_time()->push_back(ti);
			 for(itcount=input.fertlist->var.begin();itcount!=input.fertlist->var.end();itcount++){
					 idstring=itcount->first;
					 if((idstring!="year")&&(idstring!="mon")&&(idstring!="day")){	
						 if(input.fertrot->ptr->check_for_keys(idstring)) input.fertrot->ptr->set_key_empty(idstring);
						 input.fertrot->ptr->point_val(idstring)->push_back(input.fertlist->var[idstring][count]);
					 }
			 }
			 count++;
		}
		while(count<=lastentry_pos);
		
		write_N_C_into_fertrot(input);
		
		
		return 0;
 }
 int read_fertrotation::read_sitespecific_fert_history_with_year_date(
	 IN_read_fertrotation &input,int firstentry_pos,int lastentry_pos)
 {;return 0;}
 int read_fertrotation::write_N_C_into_fertrot(IN_read_fertrotation &input)
	{
		unsigned int row;
		int fertid;
		double drymatterc;
		double fertamount;
		double N_content;
		double C_content;
		
		input.fertrot->ptr->set_key("N[kg/ha]");
		input.fertrot->ptr->set_key("C[t/ha]");
		
		for(row=0;row<input.fertrot->ptr->get_size();row++)
		{
			fertid=(int)input.fertrot->ptr->get_val("fert_id",row);
			fertamount=input.fertrot->ptr->get_val("amount[tFM/ha]",row);
			drymatterc=input.fertrot->ptr->get_val("Dry_matter_content[t/t]",row);
			if (drymatterc<0)
				drymatterc=par_fert_props.get_val("Dry_matter_content[t/t]",fertid);
			N_content=input.fertrot->ptr->get_val("N_content[kg/t]",row);
			if (N_content<0)
				N_content=par_fert_props.get_val("N_content[kg/t]",fertid);
			C_content=input.fertrot->ptr->get_val("C_content[t/t]",row);
			if (C_content<0)
				C_content=par_fert_props.get_val("C_content[t/t]",fertid);

			input.fertrot->ptr->set_val("C[t/ha]",row,drymatterc*C_content*fertamount);
			input.fertrot->ptr->set_val("N[kg/ha]",row,drymatterc*N_content*fertamount);
		}
		return 0;
	}
	